package com.doyens.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doyens.common.R;
import com.doyens.dto.EmployeeDto;
import com.doyens.entity.Dept;
import com.doyens.entity.Employee;
import com.doyens.entity.Post;
import com.doyens.service.DeptService;
import com.doyens.service.EmployeeService;
import com.doyens.service.PostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/employee")
@Api(tags="员工管理接口")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private PostService postService;

    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        String password = employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

        if (emp==null){
            return R.error("登陆失败！");
        }
        if (!emp.getPassword().equals(password)){
            return R.error("登陆失败！");
        }
        if (emp.getStatus()==0){
            return R.error("账号已禁用");
        }
        request.getSession().setAttribute("employee",emp.getEmpId());

        return R.success(emp);

    }
    @PostMapping("/logout")
    @ApiOperation(value = "退出登录")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @GetMapping("/page")
    @ApiOperation(value = "查询员工方法")
    public R<Page> page(int page, int pageSize, String name){

        Page<Employee> pageInfo = new Page<>(page,pageSize);
        Page<EmployeeDto> employeeDtoPage = new Page<>();

        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,Employee::getEmpName,name);
        queryWrapper.orderByAsc(Employee::getEmpId);

        employeeService.page(pageInfo,queryWrapper);

        BeanUtils.copyProperties(pageInfo,employeeDtoPage,"records");
        List<Employee> records = pageInfo.getRecords();
        List<EmployeeDto> list = records.stream().map((item) -> {

            EmployeeDto employeeDto = new EmployeeDto();
            BeanUtils.copyProperties(item,employeeDto);

            Integer deptId = item.getDeptId();
            Dept dept= deptService.getById(deptId);

            Integer postId = item.getPostId();
            Post post= postService.getById(postId);

            if(dept != null){
                String deptName = dept.getDeptName();
                employeeDto.setDeptName(deptName);
            }
            if(post != null){
                String postName = post.getPostName();
                employeeDto.setPostName(postName);
            }
            return employeeDto;
        }).collect(Collectors.toList());
        employeeDtoPage.setRecords(list);

        return R.success(employeeDtoPage);
    }
    @ApiOperation(value = "新增员工方法")
    @PostMapping
    public R<String> save(Employee employee){
        log.info("新增员工，员工信息：{}",employee.toString());
        String password = employee.getPassword();
        employee.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        boolean save = employeeService.save(employee);
        if (save){
            return R.success("新增员工成功");
        }
        return R.error("新增员工失败...");
    }
    @ApiOperation(value = "修改员工方法")
    @PutMapping
    public R<String> update(Employee employee){
        log.info(employee.toString());
        boolean update = employeeService.updateById(employee);
        if (update){
            return R.success("员工信息修改成功");
        }
        return R.error("修改员工信息失败..");
    }

    @ApiOperation(value = "id查询员工方法")
    @GetMapping("/{id}")
    public R<EmployeeDto> getById(@PathVariable Integer id){
        log.info("根据id查询员工信息...");
        Employee employee = employeeService.getById(id);
        EmployeeDto employeeDto = new EmployeeDto();
        BeanUtils.copyProperties(employee,employeeDto);
        //查询员工对应部门
        Integer deptId = employee.getDeptId();
        Dept dept = deptService.getById(deptId);
        if (dept != null){
            String deptName = dept.getDeptName();
            employeeDto.setDeptName(deptName);
        }
        //查询对应职位信息
        Integer postId = employee.getPostId();
        Post post = postService.getById(postId);
        if (post != null){
            String postName = post.getPostName();
            employeeDto.setPostName(postName);
        }
        if(employee != null){
            return R.success(employeeDto);
        }
        return R.error("没有查询到对应员工信息");

    }
    @DeleteMapping
    @ApiOperation(value = "删除员工方法")
    public R<String> delete(Integer id){
        log.info("删除的员工id为：{}",id);
        boolean remove = employeeService.removeById(id);
        if (remove){
            return R.success("删除员工成功");
        }
        return R.error("删除员工失败...");
    }

}
